package org.leo.dm.example.reader;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.leo.dm.common.bean.StepResultBean;
import org.leo.dm.component.reader.Reader;
import org.leo.dm.util.ToUtil;

public class ProgrammMultilistReader extends Reader {

	@Override
	public StepResultBean call() {
		StepResultBean resultBean = new StepResultBean();
		try {
			getCount();

			String queueNumber = UUID.randomUUID().toString();
			storage.setStorageName(queueNumber);

			storage.putStartQueue(queuePlugin.handle(storage.getStorageName()), null);

			String[] dataValuesTemp = null;
			String segmentName = null;
			for (int i = 0; i < this.getRowCount(); i++) {
				counter++;// 计数器累加
				seqmentcounter++;// 段计数器累加

				if (i % 3 == 0) {
					segmentName = "di_test";
				} else {
					segmentName = "dw_test";
				}
				segmentPlugin.handle(segmentName);
				storage.putStartSeqment(i, segmentName, dataNames, null);
				seqmentcounter = 0;

				dataValuesTemp = new String[dataValues.length];
				for (int si = 0; si < dataValues.length; si++) {
					dataValuesTemp[si] = columnPlugin.handle(segmentName, dataNames[si], dataValues[si]);
				}
				storage.putData(recordPlugin.handle(segmentName, dataNames, dataValuesTemp, seqmentcounter));

				storage.putEndSeqment(i);
			}

			storage.putEndQueue();

			ToUtil.to("数据抽取执行完成");

			Map<String, Object> callBackMap = new HashMap<String, Object>();
			callBackMap.put("counter", counter);
			callBack.handle(callBackMap);

			resultBean.setCode("1");
			resultBean.setName("运行正常完成");
			resultBean.setSuccessCount(counter);
		} catch (Exception e) {
			e.printStackTrace();
			resultBean.setCode("-1");
			resultBean.setName("运行发生异常");
			try {
				this.getFlow().stop();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {

			counter = 0;// 计数器清零
		}

		return resultBean;
	}

	/**
	 * 
	 * Created on 2015年11月11日 .
	 * 
	 * @author xiao_h 获取总数
	 */
	private void getCount() {
		ToUtil tu1 = new ToUtil();
		ToUtil.to("count:" + rowCount);
		tu1.takeTimePrex("获取记录数花费时间");
	}

}
